package com.sk.waste.app.adapters

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.net.Uri
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.target.Target
import com.sk.waste.app.R
import com.sk.waste.app.config.NetWorkConfig
import com.sking.album.model.SKRImageModel
import java.io.File
import java.net.MalformedURLException


/**
 *  @time 2025/10/22  15:28
 *  @author Im_jingwei
 *  @desc 图片选择
 */
class AlbumAdapter(val mContext: Context, val stateType: Int, val list: ArrayList<SKRImageModel>, val onItemClick: OnItemClickLitener) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val TYPE_ADD = 0
    private val TYPE_IMAGE = 1

    override fun getItemCount(): Int {
        return (list.size + 1)
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == TYPE_ADD) {
            AddButtHolder(LayoutInflater.from(mContext).inflate(R.layout.item_album_add_butt, viewGroup, false))
        } else {
            ItemHolder(LayoutInflater.from(mContext).inflate(R.layout.item_album_image, viewGroup, false))
        }
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        when (holder) {
            is AddButtHolder -> {
                setDataAdd(holder, position)
            }

            is ItemHolder -> {
                setDataImage(position, holder)
            }
        }

    }

    override fun getItemViewType(position: Int): Int {
        return if (position == list.size) TYPE_ADD else TYPE_IMAGE
    }

    private fun setDataAdd(holder: AddButtHolder, position: Int) {
        if (stateType == 0) {
            holder.addButton.visibility = View.GONE
        } else {
            holder.addButton.visibility = View.VISIBLE
        }
        holder.addButton.setOnClickListener { onItemClick.onItemClick(position) }
    }

    private fun setDataImage(position: Int, holder: ItemHolder) {
        if (stateType == 0) {
            holder.deleteButt.visibility = View.GONE
        } else {
            holder.deleteButt.visibility = View.VISIBLE
        }
        Log.d("position", "position $position")
        val urlPath = list[position].imgurl
        if (TextUtils.isEmpty(urlPath)) return
        if (urlPath.startsWith("https://") || urlPath.startsWith("http://")) //网络图片
        {
            try {
                Glide.with(mContext).load(NetWorkConfig.NET_URL_IMA_ROOT + list[position].imgurl).transform(CenterCrop(), RoundedCorners(20))
                    .error(R.mipmap.img_item_default)
                    .into(holder.imageView);
            } catch (e: MalformedURLException) {
                e.printStackTrace()
            }
        } else  //本地图片
        {
//            val bitmap = BitmapFactory.decodeFile(list[position].imgurl)
//            holder.imageView.setImageBitmap(bitmap)
            val imageUri = File(list[position].imgurl)
            Glide.with(mContext).load(imageUri).into(holder.imageView)
        }
        holder.imageView.setOnClickListener { onItemClick.onItemClick(position) }
        holder.deleteButt.setOnClickListener { onItemClick.onItemDelete(position) }
    }

    /**
     * ItemClick的回调接口
     */
    interface OnItemClickLitener {
        fun onItemClick(position: Int)

        fun onItemDelete(position: Int)
    }

    /**
     * addbuttview
     */
    class AddButtHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val addButton: ImageButton = itemView.findViewById(R.id.album_add_button)
    }

    /**
     * imgView
     */
    class ItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.album_item_image_view)
        val deleteButt: ImageButton = itemView.findViewById(R.id.album_item_image_delete)
    }

}